import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useMusicStore = defineStore('music', () => {
  // 状态
  const currentPlaylist = ref([])
  const currentIndex = ref(0)
  const isPlaying = ref(false)
  const volume = ref(0.8)
  const isMuted = ref(false)
  const repeatMode = ref('none') // none, one, all
  const shuffleMode = ref(false)
  const currentTime = ref(0)
  const duration = ref(0)

  // 计算属性
  const currentMusic = computed(() => {
    if (
      currentPlaylist.value.length === 0 ||
      currentIndex.value < 0 ||
      currentIndex.value >= currentPlaylist.value.length
    ) {
      return null
    }
    return currentPlaylist.value[currentIndex.value]
  })

  const hasNext = computed(() => {
    if (currentPlaylist.value.length === 0) return false
    if (repeatMode.value === 'all') return true
    return currentIndex.value < currentPlaylist.value.length - 1
  })

  const hasPrevious = computed(() => {
    if (currentPlaylist.value.length === 0) return false
    return currentIndex.value > 0
  })

  // 方法
  const setPlaylist = (playlist, index = 0) => {
    // 使用深拷贝避免引用共享问题
    currentPlaylist.value = [...playlist]
    currentIndex.value = index
  }

  const addToPlaylist = (music) => {
    const exists = currentPlaylist.value.find((item) => item.id === music.id)
    if (!exists) {
      currentPlaylist.value.push(music)
    }
  }

  const removeFromPlaylist = (index) => {
    if (index >= 0 && index < currentPlaylist.value.length) {
      currentPlaylist.value.splice(index, 1)
      if (currentIndex.value >= currentPlaylist.value.length) {
        currentIndex.value = Math.max(0, currentPlaylist.value.length - 1)
      }
    }
  }

  const play = () => {
    if (currentMusic.value) {
      isPlaying.value = true
    }
  }

  const pause = () => {
    isPlaying.value = false
  }

  const togglePlay = () => {
    if (isPlaying.value) {
      pause()
    } else {
      play()
    }
  }

  const next = () => {
    if (currentPlaylist.value.length === 0) return

    if (shuffleMode.value) {
      const randomIndex = Math.floor(Math.random() * currentPlaylist.value.length)
      currentIndex.value = randomIndex
    } else {
      if (currentIndex.value < currentPlaylist.value.length - 1) {
        currentIndex.value++
      } else if (repeatMode.value === 'all') {
        currentIndex.value = 0
      }
    }
  }

  const previous = () => {
    if (currentPlaylist.value.length === 0) return

    if (shuffleMode.value) {
      const randomIndex = Math.floor(Math.random() * currentPlaylist.value.length)
      currentIndex.value = randomIndex
    } else {
      if (currentIndex.value > 0) {
        currentIndex.value--
      } else if (repeatMode.value === 'all') {
        currentIndex.value = currentPlaylist.value.length - 1
      }
    }
  }

  const seekTo = (time) => {
    currentTime.value = Math.max(0, Math.min(time, duration.value))
  }

  const setVolume = (newVolume) => {
    volume.value = Math.max(0, Math.min(1, newVolume))
  }

  const toggleMute = () => {
    isMuted.value = !isMuted.value
  }

  const setRepeatMode = (mode) => {
    repeatMode.value = mode
  }

  const toggleShuffle = () => {
    shuffleMode.value = !shuffleMode.value
  }

  const clearPlaylist = () => {
    currentPlaylist.value = []
    currentIndex.value = 0
    isPlaying.value = false
  }

  const setCurrentTime = (time) => {
    currentTime.value = time
  }

  const setDuration = (newDuration) => {
    duration.value = newDuration
  }

  return {
    // 状态
    currentPlaylist,
    currentIndex,
    isPlaying,
    volume,
    isMuted,
    repeatMode,
    shuffleMode,
    currentTime,
    duration,

    // 计算属性
    currentMusic,
    hasNext,
    hasPrevious,

    // 方法
    setPlaylist,
    addToPlaylist,
    removeFromPlaylist,
    play,
    pause,
    togglePlay,
    next,
    previous,
    seekTo,
    setVolume,
    toggleMute,
    setRepeatMode,
    toggleShuffle,
    clearPlaylist,
    setCurrentTime,
    setDuration,
  }
})
